package com.jusu.util;

import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {

	private static final String RESOURCE = "config.xml";
	private static SqlSessionFactory factory = null;
	private static SqlSession session = null;
	
	static{
		try {
			Reader reader = Resources.getResourceAsReader(RESOURCE);
			factory = new SqlSessionFactoryBuilder().build(reader);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	
	public static SqlSession getSession(){
		try{
			session = factory.openSession();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return session;
	}
	
	public static void closeSession(SqlSession session){
		session.close();
	}
}
